<template>
  <el-container class='elheight'>
    <el-aside class='appTree'>
      <app-tree :tree-data="treeData" v-on:appTreeClick="appTreeClick"></app-tree>
    </el-aside>
    <el-main>
      <el-container>
        <el-header class="dzy-main-header">
          <div class="btn-group-el">
            <btn-group :currentSelectedData="currentSelectedData" @btnClickCallback="btnClickCallback"></btn-group>
          </div>
          <div class="dzy-custom-search-box">
            <div>
              名称：<el-input placeholder="请输入内容" v-model="selectForm.countrysideName" clearable class="dzy-custom-input"></el-input>
            </div>
            <div>
              联系方式：<el-input placeholder="请输入内容" v-model="selectForm.countrysidePhone" clearable class="dzy-custom-input"></el-input>
            </div>
            <div>
              <el-button @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button>
            </div>
          </div>
        </el-header>
        <el-main>
          <app-table
            :page-data="pageData"
            :table-columns="tableColumns"
            v-on:currentChange="handleCurrentChange"
            v-on:handleCheckedRows="handleCheckedRows">
          </app-table>
          <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" class="custom-dialog_style_twoLine" :before-close="cancel" :close-on-click-modal="false">
            <el-form :model="formData"
                     :rules="rules"
                     ref="formData"
                     :disabled="showOnly"
                     label-width="50%">
              <el-form-item label="城乡社区名称：" prop="countrysideName" label-width="30%">
                <el-input v-model="formData.countrysideName" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="联系方式：" prop="countrysidePhone" label-width="30%">
                <el-input v-model="formData.countrysidePhone" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="面积（m²）：" label-width="30%">
                <el-input v-model="formData.countrysideArea" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="人口数量：" label-width="30%">
                <el-input v-model="formData.countrysidePopulation" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="视频监控摄像机总数：" prop="publicSafeVideo" label-width="30%">
                <el-input v-model="formData.publicSafeVideo" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="高清摄像机总数：" label-width="30%">
                <el-input v-model="formData.hdVideoCamera" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="与乡镇(街道)视联网信息中心联网摄像机数量：" label-width="30%">
                <el-input v-model="formData.linkCenter" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="24小时值守摄像机数量：" prop="alwaysWork" label-width="30%">
                <el-input v-model="formData.alwaysWork" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="每平方公里视频监控摄像机数量：" label-width="30%">
                <el-input v-model="formData.everyKilometer" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="每百人视频监控摄像机数量：" label-width="30%">
                <el-input v-model="formData.everyHundred" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="行政区：" prop="regionIdArr">
                <div class="block">
                  <el-cascader
                    placeholder="请选择"
                    :options="regionOptions"
                    filterable
                    clearable
                    :props="areaProps"
                    change-on-select
                    v-model="formData.regionIdArr">
                  </el-cascader>
                </div>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="isfooterBtnShow">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="save('formData')">确 定</el-button>
            </div>
          </el-dialog>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import AppTree from '@/components/common/AppTree'
import AppTable from '@/components/AppTable'
import BtnGroup from '@/components/BtnGroup'
import {getAllRegion} from '@/utils/region'
import {getRegionTree} from '@/http/AppApi'
import {findCountrysideVideo, createCountrysideVideo, updateCountrysideVideo, delCountrysideVideo} from '@/http/OrgApi'
export default {
  data () {
    return {
      dialogFormVisible: false,
      isfooterBtnShow: true,
      pageData: {},
      treeData: [],
      dialogTitle: '',
      currentSelectedData: [],
      showOnly: false,
      tableColumns: [
        { prop: 'countrysideName', label: '城乡社区名称' },
        { prop: 'countrysidePhone', label: '联系方式' },
        { prop: 'countrysideArea', label: '面积(m²)' },
        { prop: 'countrysidePopulation', label: '人口' },
        { prop: 'publicSafeVideo', label: '视频监控摄像机总数' }
      ],
      tableDada: [],
      regionOptions: [],
      formData: {},
      selectForm: {},
      areaProps: {
        label: 'name',
        value: 'id'
      },
      rules: {
        countrysideName: [
          { required: true, message: '请输入社区名称：', trigger: 'blur' }
        ],
        countrysidePhone: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        publicSafeVideo: [
          { required: true, message: '请输入视频监控摄像机', trigger: 'blur' }
        ],
        alwaysWork: [
          { required: true, message: '请输入24小时值守摄像机数量', trigger: 'blur' }
        ],
        regionIdArr: [
          { type: 'array', required: true, message: '请选择行政区', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    AppTree,
    BtnGroup,
    AppTable
  },
  mounted () {
    getRegionTree().then(resp => {
      if (resp.code && resp.code === '200') {
        this.treeData = resp.data
        this.regionOptions = resp.data
      } else {
        this.$message({
          type: 'info',
          message: resp.message
        })
        return ''
      }
    })
    this.getTableData()
  },
  methods: {
    save (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          this.$alert('请填写正确表单格式', '', {
            confirmButtonText: '确定'
          })
          return ''
        } else {
          if (this.formData.regionIdArr) {
            this.formData.allRegionId = '0,'
            for (let i = 0; i < this.formData.regionIdArr.length; i++) {
              this.formData.allRegionId += this.formData.regionIdArr[i] + ','
            }
            this.formData.allRegionId = this.formData.allRegionId.substring(0, this.formData.allRegionId.length - 1)
          }
          if (this.formData.id) {
            updateCountrysideVideo(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                this.getTableData()
              } else {
                this.$message({
                  type: 'info',
                  message: resp.message
                })
              }
            })
          } else {
            createCountrysideVideo(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.getTableData()
              } else {
                this.$message({
                  type: 'info',
                  message: resp.message
                })
              }
            })
          }
        }
      })
    },
    del (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.forEach(row => {
            delCountrysideVideo(row).then(resp => {
              if (resp.code && resp.code === '200') {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this.getTableData()
              } else {
                this.$message({
                  type: 'info',
                  message: resp.message
                })
              }
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    cancel () {
      this.dialogFormVisible = false
      this.formData = {}
    },
    getTableData (param) {
      findCountrysideVideo(param).then(resp => {
        if (resp.code && resp.code === '200') {
          this.pageData = resp.data
          if (this.pageData.items && this.pageData.items instanceof Array) {
            this.tableData = this.pageData.items
          }
        } else {
          this.$message({
            type: 'info',
            message: resp.message
          })
          return ''
        }
      })
    },
    handleCurrentChange (val) {
      this.selectForm.offset = (val - 1) * 10
      this.getTableData(this.selectForm)
    },
    btnClickCallback (callbackData) {
      if (callbackData.btnType === 'delete') {
        this.dialogFormVisible = false
        let rows = callbackData.rows
        this.del(rows)
      } else {
        this.isfooterBtnShow = callbackData.btnType !== 'get'
        if (callbackData.btnType === 'post') {
          this.dialogTitle = '新增'
          if (this.formData.regionIdArr) {
            let arr = this.formData.regionIdArr
            this.formData = {}
            this.formData.regionIdArr = arr
          } else {
            this.formData = {}
          }
        } else {
          this.dialogTitle = callbackData.btnType === 'put' ? '修改' : '查看'
          let obj = {}
          obj = JSON.stringify(callbackData.rows[0])
          this.formData = JSON.parse(obj)
          let regionData = {}
          regionData.id = this.formData.regionId
          regionData.items = this.regionOptions
          this.formData.regionIdArr = getAllRegion(regionData)
        }
        this.showOnly = callbackData.sohwOnly
        this.dialogTitle = callbackData.dialogTitle
        this.dialogFormVisible = true
      }
    },
    handleCheckedRows (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.currentSelectedData = rows
        this.formData = rows[0]
      } else if (rows.length === 0) {
        this.currentSelectedData = []
      }
    },
    appTreeClick (data) {
      let regionData = {}
      regionData.id = data.id
      regionData.items = this.regionOptions
      this.formData.regionIdArr = getAllRegion(regionData)
      this.selectForm.regionId = data.id
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    },
    searchHandleClick () {
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    }
  }
}
</script>
